package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.web.interceptor.AdminHolder;
import com.itheima.web.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@RestController
public class AdminController {

    @Autowired
    private AdminManager adminManager;

    @GetMapping("/system/users/verification")//生成验证码
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptche=adminManager.verification(uuid);
        lineCaptche.write(response.getOutputStream());
    }

    @PostMapping("/system/users/login")//登录
    public ResponseEntity login(@RequestBody Map<String ,String >map){
        String username = map.get("username");
        String password = map.get("password");
        String code = map.get("verificationCode");
        String uuid = map.get("uuid");
        return adminManager.login(username,password,code,uuid);
    }

    @PostMapping("/system/users/profile")// 管理员信息
    public ResponseEntity profile(){
        Admin admin=AdminHolder.get();
        return ResponseEntity.ok(admin);
    }

    @PostMapping("/system/users/logout")//退出登录
    public void logout(@RequestHeader("Authorization") String token){
        adminManager.logout(token);
    }
}
